const GWChttp = require("../../utils/ClassGWC")
Page({

  
    data: {
       carts:[],
       allChecked:true,
       sum:""

    },
    async onLoad(){
        var res = await GWChttp.gethome();
        this.setData({
            carts:res.data.results,
        })
        this.handleSum()
    },
    handleNum(event){
        // 通过id值，找到数组中对应的项目，将项目中的productCount
        //console.log(event.detail)
        var carts = this.data.carts
        var {aid}  =event.currentTarget.dataset;
        carts.forEach(item=>{
            if(item.id ==aid)[
                item.count.num = event.detail
            ]
        })
        this.setData({
            carts
        })
        this.handleSum()
    },
    handleSelect(event){
        console.log(event.detail);
        var carts = this.data.carts
        var {aid}  =event.currentTarget.dataset;
        carts.forEach(item=>{
            if(item.id ==aid)[
                item.checked = event.detail
            ]
        })

        var allChecked = carts.every(item=>{
            return item.checked
        })
        this.setData({
            carts,
            allChecked
        })
        this.handleSum()
    },
    handleAll(event){
        console.log(event.detail);
        var carts = this.data.carts
        carts.forEach(item=>{
            item.checked =event.detail
        })
        this.setData({
            allChecked:event.detail,
            carts
        })
        this.handleSum()
    },
    handleSum(){
        var sum = 0;
        var carts = this.data.carts;
        carts.forEach(item=>{
            if(item.checked){
            sum+=(item.price)*(item.count.num)
            }
        })
        this.setData({
            sum:sum*100
        })
    }
})